﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace TestMvc.Controllers
{
  public class HomeController : AsyncController
  {
    public ActionResult Index()
    {
      ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

      return View();
    }
    public ActionResult Ajax()
    {
      return View();
    }
    [HttpPost]
    public ActionResult Ajax(int id)
    {
      if (id > 0)
      {
        Thread.Sleep( 1000);
      }
      return Content(DateTime.Now.ToString());
    }
    public void AboutAsync()
    {
      AsyncManager.OutstandingOperations.Increment();
      AsyncManager.Parameters["dateStart"] = DateTime.Now.ToString();
      AsyncManager.Sync(() =>
      {
        Thread.Sleep(10000);
        AsyncManager.Parameters["date"] = DateTime.Now.ToString();
      }
  );
      AsyncManager.OutstandingOperations.Decrement();

    }
    public ActionResult AboutCompleted(string dateStart, string date)
    {
      ViewBag.Message = "Your app description page.";
      var i = 1000000000.123;
      return View();
    }

    public ActionResult Contact(int type = 0)
    {
      ViewBag.Message = "Your contact page.";
      StringBuilder sb = new StringBuilder();

      if (type == 0)
      {
        Thread.Sleep(60000);
      }
      else if (type == 1)
      {
        int min, max, current, currentMax, available, availableT;
        ThreadPool.GetMinThreads(out min, out current);
        ThreadPool.GetMaxThreads(out max, out currentMax);
        ThreadPool.GetAvailableThreads(out available, out availableT);
        sb.AppendFormat("{0}-{1}-----", min, current);
        sb.AppendFormat("{0}-{1}-----", max, current);
        sb.AppendFormat("{0}-{1}-----", available, availableT);
        ThreadPool.SetMinThreads(1, 1);
        ThreadPool.GetMinThreads(out min, out current);
        ThreadPool.GetMaxThreads(out max, out currentMax);
        ThreadPool.GetAvailableThreads(out available, out availableT);
        sb.AppendFormat("<br/>{0}-{1}-----", min, current);
        sb.AppendFormat("{0}-{1}-----", max, current);
        sb.AppendFormat("{0}-{1}-----", available, availableT);
        ThreadPool.SetMaxThreads(2, 1);
        ThreadPool.GetMinThreads(out min, out current);
        ThreadPool.GetMaxThreads(out max, out currentMax);
        ThreadPool.GetAvailableThreads(out available, out availableT);
        sb.AppendFormat("<br/>{0}-{1}-----", min, current);
        sb.AppendFormat("{0}-{1}-----", max, current);
        sb.AppendFormat("{0}-{1}-----", available, availableT);

        sb.AppendFormat("<br/>{0}", DateTime.Now.ToString());
      }
      else
      {
        int min, max, current, currentMax, available, availableT;
        ThreadPool.GetMinThreads(out min, out current);
        ThreadPool.GetMaxThreads(out max, out currentMax);
        ThreadPool.GetAvailableThreads(out available, out availableT);
        sb.AppendFormat("{0}-{1}-----", min, current);
        sb.AppendFormat("{0}-{1}-----", max, current);
        sb.AppendFormat("{0}-{1}-----", available, availableT);

        sb.AppendFormat("<br/>{0}", DateTime.Now.ToString());
      }
      return Content(sb.ToString());
    }
  }
}
